package com.cskaoyan.controller.wx;

import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.comment.Comment;
import com.cskaoyan.bean.goods.GoodsComment;
import com.cskaoyan.bean.userbean.User;
import com.cskaoyan.service.goods.GoodsService;
import com.cskaoyan.service.userservice.UserService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.goodsvo.GoodsComment_TotalVo;
import com.cskaoyan.vo.uservo.UserVo;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ked
 * @createTime 2021年05月12日 10:44:00
 */

@RestController
public class WxCommentController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    UserService userService;


    /**
     * 商品页面-评论列表
     */
    @GetMapping("wx/comment/list")
    public BaseRespVo getCommentList(Integer valueId,
                                     Byte type, Integer size,
                                     Integer page, Integer showType){

        Map<String,Object> comments = new HashMap<>();
        List<GoodsComment> goodsComments = goodsService.queryComment(page,size,type,valueId);
        comments.put("count",goodsComments.size());

        comments.put("currentPage",page);

        List<Map<String,Object>> data = new ArrayList<>();
        for (GoodsComment goodsComment : goodsComments) {
            Map<String,Object> commentsVo = new HashMap<>();
            commentsVo.put("addTime",goodsComment.getAddTime());
            commentsVo.put("content",goodsComment.getContent());
            commentsVo.put("picList",goodsComment.getPicUrls());
            commentsVo.put("star",goodsComment.getStar());
            Map<String,String> userInfo = userService.query(goodsComment.getUserId());
            commentsVo.put("userInfo",userInfo);
            data.add(commentsVo);
        }
        comments.put("data",data);
        return BaseRespVo.ok(comments,"成功");
    }

    /**
     * 评论页面，所有评论和带图评论统计
     */
    @GetMapping("wx/comment/count")
    public BaseRespVo getCommentCount(@NotNull Integer valueId,@NotNull Byte type){

        Map<String,Integer> data = goodsService.queryCommentCount(valueId,type);
        data.put("allCount",data.get("allCount"));
        data.put("hasPicCount",data.get("hasPicCount"));
        return BaseRespVo.ok(data,"成功");
    }

    @PostMapping("wx/comment/post")
    public Object post(@RequestBody Comment comment) {

        Integer userId = MyUtils.getUserId();
        comment.setUserId(userId);
        Comment comment1 = goodsService.insertComment(comment);
        return BaseRespVo.ok(comment1);
    }



}
